#!/bin/sh
#
# A regression test script for hexdump(1)'s format string functionality.
#
# The compilation of software known as hexdump is distributed under the
# following terms:
#
# Copyright (c) 1992-2010 hexdump Project. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
# 
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#

. "$(dirname "$0")/lib.sh"

setup() {
	TCID="test_format"
	TST_TOTAL=6
	PROG=$(realpath "$0")
	: ${hexdump=hexdump}
	type "$hexdump" >/dev/null && tst_tmpdir
}

teardown() {
	trap : EXIT
	cd /
	rm -Rf "${TST_TMPDIR}" "${OUTPUT_DIR}"
	exit ${TEC}
}

trap teardown EXIT
setup || exit $?

do_positive_test()
{

	tst_count=$1; shift

	rc=$TFAIL

	tst_resm TINFO "Executing: \`$hexdump $@'"

	expected="$TST_TMPDIR/$tst_count-exp.log"
	obtained="$TST_TMPDIR/$tst_count.log"

	if "$hexdump" "$@" > "$obtained"; then

		# Diff output with expected output.

		"${PROG%/*}/mini_hexdump.py" "$@" > "$expected"
		ec=$?
		if [ $ec -ne 0 ] ; then
			tst_resm TFAIL "mini_hexdump.py '$@' exited with a non-zero status ($ec) unexpectedly"
		elif diff "$expected" "$obtained" ; then
			tst_resm TPASS "hexdump '%@' passed as expected."
			rc=$TPASS
		else
			tst_resm TFAIL "hexdump '$@' failed (expected output didn't match)."
		fi
	else
		tst_resm TFAIL "hexdump '$@' failed unexpectedly."
	fi

	return $rc

}

#
# Positive tests.
#
# 1. Ensure positive functionality via the manpage is correct.
# 2. Resolve the following issues grabbed from:
# http://www.freebsd.org/cgi/query-pr.cgi?pr=bin/45529
# - '/1 "%03"'
# - '/1 "\%o"'
# - '/1 "\t%03o"'
# - '/1 "\\%03o"'
# - '2/1 ""'
# 3. Resolve the following issue grabbed from:
# http://www.freebsd.org/cgi/query-pr.cgi?pr=bin/81495
# - '"" /1 "%%%02x" "\n"'
# 4. Resolve the following issues grabbed from:
# http://www.netbsd.org/cgi-bin/query-pr-single.pl?number=28157
# - '8/1 "\\x%02x" "\n"'
# - '1/1 "\\x%02x"'
# - '1/1 "\nx"'

: $(( TST_COUNT += 1 ))
jot -w "%07d" 32 0 | tr '\012' '\040' > "$TST_TMPDIR/scenario.1"
do_positive_test $TST_COUNT -e '/32 "%s\\n"' "$TST_TMPDIR/scenario.1"
: $(( TEC |= $? ))
rm -f "$TST_TMPDIR/scenario.1"

for parg in '"%06.6_ao "  12/1 "%3_u "' '"\\t\\t" "%_p "' '"\\n"' '"%07.7_Ax\\n"' '"%07.7_ax  " 8/2 "%04x " "\\n"' '8/x "%0.4" "\\n"' '/1 "\\%03o"' '/1 "\\t%03o"' '/1 "%03o"' '2/1 ""' '1/2 "%%foo"' '/1 "%%%02x" "\\n"' '/1 "\%o"' '8/1 "\\x%02x" "\\n"' '1/1 "\\x%02x"' '1/1 "\\nx"' '/1 "%s\\n"' ; do
	: $(( TST_COUNT += 1 ))
	do_positive_test $TST_COUNT -e "$parg" "$PROG"
	: $(( TEC |= $? ))
done

#
# Negative tests.
#
# TODO: Comment on test stimuli.
#
for narg in '"%4s"' '-1/1 "%01_ao"' '1/-1 "%01_ao"' "%" '/1 "%03"' '"%s"'; do

	tst_resm TINFO "Executing: \`$hexdump -e '$narg''"

	: $(( TST_COUNT += 1 ))

	if "$hexdump" -e "$narg" "$hexdump"; then
		tst_resm TFAIL "hexdump -e '$narg' passed unexpectedly."
	else
		if [ "x$(ls *.core 2>/dev/null)" = x ] ; then
			tst_resm TPASS "hexdump -e '$narg' failed as expected."
		else
			tst_resm TFAIL "hexdump -e '$narg' dumped core."
			: $(( TEC |= $TFAIL ))
		fi
	fi

	# Cleanup all coredumps
	rm -f *.core

done
